<template>
  <div :class="className" :style="{ height: height, width: width}"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/shine') // echarts theme
export default {
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '250px'
    },
    className: {
      type: String,
      default: 'chart',
    },
    // titleText: {
    //   type: String,
    //   default: '源资产排行',
    // },
    yTitleData: {
      type: Array,
    },
    data: {
      type: Array,
    },
  },
  watch: {
    yTitleData(val) {
      this.yTitleData = val
      this.drawEcharts()
    },
    data(val) {
      this.data = val
      this.drawEcharts()
    },
    titleText(val) {
      this.titleText = val
      this.drawEcharts()
    },
  },

  data() {
    return {
      chart: null,
    }
  },
   mounted() {
    this.$nextTick(() => {
      this.drawEcharts()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    drawEcharts() {
      this.chart = echarts.init(this.$el, 'shine')
      var option = {
        // title: {
        //   left: 'left',
        //   text: this.titleText,
        //   textStyle: {
        //     fontSize: 15,
        //     fontWeight: 'bolder',
        //   },
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        legend: {
          left: 'right',
          top: '1%',
          itemHeight: 6,
          itemWidth: 15,
          textStyle: {
            fontSize: 8,
          },
        },
        grid: {
          left: '1%',
          right: '6%',
          top: '18%',
          bottom: '2%',
          containLabel: true,
        },
        xAxis: {
          type: 'value',
          axisTick: {
            show: false,
          },
          inverse:true,
          axisLabel: {
            margin: 2,
            fontSize: 7,
            // formatter: numberFormat,
          },
        },

        yAxis: {
          type: 'category',
          data: ['科学技术界','文艺艺术界','社会科学界','经济界','农业界','教育界','体育界','医疗卫生界','其他界'],
          position:'right',
          axisLabel: {
            margin: 2,
            fontSize: 7,
            show:false
          },
          axisLine: {show:false},
          axisTick:{
          show:false // 不显示坐标轴刻度线
    },
        },
        series: [
          {
            // name: '数量',
            type: 'bar',
            itemStyle: {
              color: '#4682B4',
            },
            barWidth: '10',
            data: [50,100,150,200,150,100,50,100,150],
          },
        ],
      }
      this.chart.setOption(option)
    },
  },
//   mounted() {
//     this.drawEcharts()
//     this.__resizeHandler = debounce(() => {
//       if (this.chart) {
//         this.chart.resize()
//       }
//     }, 100)
//     window.addEventListener('resize', this.__resizeHandler)
//   },
}
</script>
